package com.zys.sac.starter.config;

import com.zys.sac.core.config.SacNewWebSecurityConfiguration;
import com.zys.sac.core.config.SimpleSacWebSecurityConfiguration;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;

/**
 * spring security配置类选择器，根据sz.sac.config.adapter配置来选择不同的配置类（主要作用是为了兼容低版本）
 *
 * Created by zhongjunkai on 2022/10/25.
 */
public class SacAutoConfigurationAdapter implements ImportSelector, EnvironmentAware {

    private Environment environment;

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        Boolean adapter = environment.getProperty("opensource.sac.config.adapter", Boolean.class, false);
        if (adapter) {
            return new String[]{SimpleSacWebSecurityConfiguration.class.getName()};
        } else {
            return new String[]{SacNewWebSecurityConfiguration.class.getName()};
        }
    }
}
